<template>
    <div style="height:100%">

        <el-alert
                title="分类添加"
                type="success">
        </el-alert>
        <br>
        <div class="cateform" style="width:50%">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="选择科目" prop="cate">
                    <el-select v-model="ruleForm.cate" placeholder="请选择科目">
                        <el-option label="科目一" value="1"></el-option>
                        <el-option label="科目二" value="2"></el-option>
                        <el-option label="科目三" value="3"></el-option>
                        <el-option label="科目四" value="4"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>

        </div>
    </div>
</template>

<script>
    import Navs from './Nav';
    import Sidebars from './Sidebar';

    export default {
        components: {
            Navs,
            Sidebars
        },
        data() {
            return {
                ruleForm: {
                    name: '',
                    cate: '',

                },
                rules: {
                    name: [
                        { required: true, message: '请输入分类名称', trigger: 'blur' },
                        { min: 3,  message: '长度在 3 到 50 个字符', trigger: 'blur' }
                    ],
                    cate: [
                        { required: true, message: '请选择科目分类', trigger: 'change' }
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {

                        axios.post('cateadd',this.ruleForm).then(rew=>{
                            if(rew.data==200){
                                this.open3()
                            }
                            if(rew.data==400){
                                this.open4()
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            open3() {
                this.$notify({
                    title: '添加成功',
                    message: '点击关闭',
                    type: 'success'
                });
            },
            open4() {
                this.$notify({
                    title: '分类已存在',
                    message: '点击关闭',
                    type: 'error'
                });
            },
        }
    }
</script>

